import { Component } from "cc";
import { SocketManager } from "./SocketManager";
import ProtoBufManager from "../../proto/ProtoBufManager";
import { ViewManager } from "../base/ViewManager";
import Log from "../../../Core/Log/Log";

export class AbstractModel extends Component {
    public constructor() {
        super();
        this.init();
        this.registerInterestedProtocols();
    }
    protected init(): void {

    }
    /**注册协议回调 */
    protected registerInterestedProtocols(): void {

    }
    /**
     * 注册协议回调
     * msg 协议号
     *  */
    public reg(msg: any, callback: Function, param1: string, param2: string): void {
        var handler: ProtocolHandler = new ProtocolHandler(msg, callback, this);
        handler.param1 = param1;
        handler.param2 = param2;
        SocketManager.ins.registerHandler(msg, handler);
    }
    public addRouteMap(msg: any, callback: Function, param1: string, param2: string): void {
        var handler: ProtocolHandler = new ProtocolHandler(msg, callback, this);
        handler.param1 = param1;
        handler.param2 = param2;
        SocketManager.ins.addRouteMap(msg, handler);
    }
    /**发送 */
    protected send(msgID: number, route: any, protocol: any): void {
        SocketManager.ins.sendRequest(msgID, route, protocol);
    }
    // public getRequest(): protoCmd.Request {
    //     return ProtoBufManager.ins.pbCmd.Request.create();
    // }
    /**
    * 重置数据
    */
    public reset(): void {

    }

    /**
     * 销毁
     */
    public dispose(): void {
    }
}
export class ProtocolHandler {

    public protocolID: any;
    public callback: Function;
    public thisObj: any;
    public param1: string;
    public param2: string;

    public constructor(protocolID: any, callback: Function, thisObj: any) {
        this.protocolID = protocolID;
        this.callback = callback;
        this.thisObj = thisObj;
    }

    public apply(protocol: any): void {
        let info: any;
        try {
            info = ProtoBufManager.ins[this.param1][this.param2].decode(protocol);
            if (info.ErrCode && info.ErrCode != ProtoBufManager.ins.common.ErrorCode.OK) {
                switch (info.ErrCode) {
                    case ProtoBufManager.ins.common.ErrorCode.GameRoomIsNotExist:
                        ViewManager.ins.showtips(`当前房间不存在`);
                        break;
                    case ProtoBufManager.ins.common.ErrorCode.GameRoomIsFull:
                        ViewManager.ins.showtips(`当前房间已满`);
                        break;
                    case ProtoBufManager.ins.common.ErrorCode.ItemNotExist:
                        ViewManager.ins.showtips(`道具不存在`);
                        break;
                    case ProtoBufManager.ins.common.ErrorCode.ItemNotEnough:
                        ViewManager.ins.showtips(`道具数量不足`);
                        break;
                    default:
                        ViewManager.ins.showtips(`操作失败`);
                        break;
                }
                Log.ins.log(3, `${this.param2} 操作失败,错误码:${info.ErrCode}`);
                return;
            }
        } catch (error) {
            try {
                Log.ins.log(4, `协议 ${this.param2} 解析出错`, ProtoBufManager.ins.common.Error.decode(protocol));
            } catch (error) {
                Log.ins.log(4, `协议 ${this.param2} 解析出错`);
            }
        }
        if (info) {
            this.callback.call(this.thisObj, info);
        }
    }
}